###################replciation file for#############################
###########Horne, Adams and Gidron CPS 2022########################
library(tidyverse)
library(estimatr)
library(stargazer)
library(jtools)
library(fixest)
library(modelsummary)
library(flextable)



`%notin%` <- Negate(`%in%`)

## Figures

##Plot 1
##Country leveld data for plots
Country_Level_Data <-read_csv("CountryLevelDataFinal.csv")
## Individual level data
load("CPS_HAG_Final.Rdata")



Country_level_Averages <-group_by(Country_Level_Data, Country) %>%
    summarize(logDM = mean(logDM), dislike = mean(Dislike_AP), Like_In_Party = mean(Like_In_Party))
p1 <-ggplot(Country_level_Averages, aes(x = logDM, y = dislike, label = Country)) + geom_point() + theme_minimal() +
    xlab("Logged District Magnitude") + ylab("Out Party Disilke") +
    theme(axis.text.x = element_text(size = 16))  + theme(axis.text.y = element_text(size = 16))
library(ggrepel)
p1 + geom_label_repel(aes(label = Country),
                      box.padding   = 0.6, 
                      point.padding = 0.6,
                      segment.color = 'grey50') + 
    geom_smooth(method='lm', formula= y~x, color = "black") + theme( axis.title=element_text(size=14))


##PLot 2

updated <-mutate(updated, coalition_last_20 = ifelse(govern_together == T | govern_together_10 == T |
                                                         govern_together_15 == T | govern_together_20 == T, 1, 0))


##Fix UK (Con - LD) should eual 1 for 2015

plot <-group_by(updated, Country = country) %>%
    summarize(`Proportion of Dyads in Coalition in Last 20 Years` = mean(coalition_last_20))
    

Country_level_Averages <-left_join(Country_level_Averages, plot)



p2 <-ggplot(Country_level_Averages, aes(x = logDM, y = `Proportion of Dyads in Coalition in Last 20 Years`, label = Country)) + geom_point() + theme_minimal() +
    xlab("Logged District Magnitude") + ylab("Proportion of Dyads in Coalition in Last 20 Years") +
    theme(axis.text.x = element_text(size = 16))  + theme(axis.text.y = element_text(size = 16))
library(ggrepel)
p2 + geom_label_repel(aes(label = Country),
                      box.padding   = 0.6, 
                      point.padding = 0.6,
                      segment.color = 'grey50') + 
    geom_smooth(method='lm', formula= y~x, color = "black") + theme( axis.title=element_text(size=14))












## Table 2


mod2.1_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20
                        + opposition_together
                    |ID, cluster = ~country, updated)
mod2.1_15yr

mod2.2_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
 + opposition_together  + rile_distance_s + to_rrparty 
                   |ID, cluster = ~country, updated)

mod2.2_15yr

mod2.3_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + opposition_together  + econ_distance_s + society_distance_s + to_rrparty 
                    |ID, cluster = ~country, updated)

mod2.3_15yr


## Table 3


mod3.1 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                 + opposition_together  + avg_rile_distance_s + to_rrparty 
             |ID, cluster = ~country, updated)

mod3.1

##
mod3.2 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together + avg_econ_distance_s + avg_society_distance_s + to_rrparty 
             |ID , cluster = ~country, updated)
mod3.2


################Analyses in SI Memo##################
##  Country-year Fixed Effects (Appendix S3.1)

S3.1_1 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20
                    + opposition_together
                    |cntryyr, cluster = ~country, updated)
S3.1_1


S3.1_2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + opposition_together  + rile_distance_s + to_rrparty 
                    |cntryyr, cluster = ~country, updated)

S3.1_2

S3.1_3 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + opposition_together  + econ_distance_s + society_distance_s + to_rrparty 
                    |cntryyr, cluster = ~country, updated)

S3.1_3

updated$gov

## Country Fixed Effects (S3.2)




cfe1 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together
             |country , cluster = ~country, updated)


cfe1




cfe2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                 + opposition_together  + rile_distance_s + to_rrparty
             |country, cluster = ~country, updated)

cfe2

##
cfe3 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together + econ_distance_s + society_distance_s + to_rrparty 
             |country, cluster = ~country, updated)

cfe3

## Party Fixed Effects (S3.3)




pfe1 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together
             |to_mp_number + from_mp_number , cluster = ~country, updated)


pfe1




pfe2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                 + opposition_together  + rile_distance_s
             |to_mp_number + from_mp_number, cluster = ~country, updated)

pfe2

##
pfe3 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together + econ_distance_s + society_distance_s
             |to_mp_number + from_mp_number , cluster = ~country, updated)

pfe3



# Different Time Periods - S4
##Split early and later years####

early <-filter(updated, year < 2007)
later <-filter(updated, year > 2007)


early1 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                   + opposition_together  + rile_distance_s + to_rrparty
               |ID, cluster = ~country, early)

early1

early2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                   + opposition_together  + econ_distance_s + society_distance_s + to_rrparty
               |ID, cluster = ~country, early)

early2

later1 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                   + opposition_together  + rile_distance_s + to_rrparty
               |ID, cluster = ~country, later)

later1

later2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                   + opposition_together  + econ_distance_s + society_distance_s + to_rrparty
               |ID, cluster = ~country, later)

later2


## Standard Errors at Country-Year Level (Appendix S5)


mod2.1_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20
                    + opposition_together
                    |ID, cluster = ~cntryyr, updated)
mod2.1_15yr

mod2.2_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + opposition_together  + rile_distance_s + to_rrparty 
                    |ID, cluster = ~cntryyr, updated)

mod2.2_15yr

mod2.3_15yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + opposition_together  + econ_distance_s + society_distance_s + to_rrparty 
                    |ID, cluster = ~cntryyr, updated)

mod2.3_15yr



## 10 year windows (appednix s6)

## combine 15 and 20 year variables into one 10 year window
updated <-mutate(updated, govern_together_20_v2 = ifelse(govern_together_20 == T | govern_together_15 == T, 1, 0))


mod2.1_10yr <-feols(thermometer_score ~ govern_together + govern_together_10  + govern_together_20_v2
                    + opposition_together
                    |ID, cluster = ~country, updated)
mod2.1_10yr

mod2.2_10yr <-feols(thermometer_score ~ govern_together + govern_together_10 +  govern_together_20 +
                        + opposition_together  + rile_distance_s + to_rrparty 
                    |ID, cluster = ~country, updated)

mod2.2_10yr

mod2.3_10yr <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_20 +
                        + opposition_together  + econ_distance_s + society_distance_s + to_rrparty 
                    |ID, cluster = ~country, updated)

mod2.3_10yr


## Excluding very proportional countries (Appendix S7)
no_prop <-filter(updated, logDM <5.5)

prop1 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20
              + opposition_together
              |ID, cluster = ~country, no_prop)
prop1

prop2 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                  + opposition_together  + rile_distance_s + to_rrparty 
              |ID, cluster = ~country, no_prop)

prop2

prop3 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                  + opposition_together  + econ_distance_s + society_distance_s + to_rrparty 
              |ID, cluster = ~country, no_prop)

prop3


##Party fixed effects, average ideological distance - appendix s8

pfe4 <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                 + opposition_together  + avg_rile_distance_s
             |to_mp_number + from_mp_number, cluster = ~country, updated)

pfe4

##
pfe5 <-feols(thermometer_score ~ govern_together + govern_together_10 + 
                 govern_together_15 + govern_together_20 + opposition_together + avg_econ_distance_s + avg_society_distance_s
             |to_mp_number + from_mp_number , cluster = ~country, updated)

pfe5


## Without Opposition Status (Appendix S9)


mod2.1_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 
                        |ID, cluster = ~country, updated)
mod2.1_15no

mod2.2_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + rile_distance_s + to_rrparty 
                    |ID, cluster = ~country, updated)

mod2.2_15no

mod2.3_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 +
                        + econ_distance_s + society_distance_s + to_rrparty 
                    |ID, cluster = ~country, updated)

mod2.3_15no




## Standard Errors at Individual Level (Appendix S10)


mod2.1_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 + opposition_together 
                    
                    |ID, cluster = ~ID, updated)
mod2.1_15no

mod2.2_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 + opposition_together +
                        + rile_distance_s + to_rrparty 
                    |ID, cluster = ~ID, updated)

mod2.2_15no

mod2.3_15no <-feols(thermometer_score ~ govern_together + govern_together_10 + govern_together_15 + govern_together_20 + opposition_together +
                        + econ_distance_s + society_distance_s + to_rrparty 
                    |ID, cluster = ~ID, updated)

mod2.3_15no
